/**
* @description: 路由器核查
* @author: JiWei
* @remark:
**/
<template>
  <view class="router-inspect-main">
    <nav-bar class="header" back title="路由器核查" @goBack="goBack"/>
    <view class="main-container">
      <!--基本信息-->
      <view class="plan-info-title">
        <plateTitle title="基本信息" class="plate-title-1"/>
      </view>
      <view class="base-info-container">
        <van-cell-group>
          <van-cell v-for="(item,index) in taskDetail.powerDetailArr" :key="index" :title="item.title" :value="item.text">
            <template #right-icon v-if="item.title === '实物ID'">
              <span class="operation" v-if="taskDetail.originData.entityid !== null" @click="scanClick">核对</span>
            </template>
          </van-cell>
        </van-cell-group>
      </view>

      <!--巡视信息-->
      <view class="plan-info-title">
        <plateTitle title="巡视信息" class="plate-title-2"/>
      </view>
      <view class="patrol-info-container">
        <van-field
            readonly
            clickable
            input-align="right"
            right-icon="arrow"
            :value="isCoincide"
            label="台账信息与现场信息是否一致"
            placeholder="点击选择"
            @click="showPicker = true"
        />
        <van-radio-group v-show="isCoincide === '不一致'"
                         v-model="operation"
                         icon-size="15px"
                         checked-color="#417ADC">
          <van-radio name="noExist">现场无此设备，将标记此设备状态为“不存在”</van-radio>
          <van-divider dashed />
          <van-radio name="modify">现场设备信息不一致，将标记此设备状态为“修改”</van-radio>
        </van-radio-group>
      </view>
    </view>
    <!--按钮-->
    <van-button block @click="confirmInfo">{{ buttonText() }}</van-button>
    <!--选择器-->
    <van-popup v-model="showPicker" position="bottom">
      <van-picker
          show-toolbar
          :columns="columns"
          @confirm="onConfirm"
          @cancel="showPicker = false"
      />
    </van-popup>
  </view>
</template>
<script>

import wx from "../../../../static/common_js/jweixin_1.0.0";

let power_obj = {
  "routerName": "设备名称",
  "parStationIdName": "所属站点",
  "parRoomName": "所属机房",
  "parRack": "所属机柜编号",
  "parRackScreen": "所属机柜屏眉",
  "dspLevelName": "设备所属通信网等级",
  "producerIdName": "设备品牌",
  "modelnumberName": "设备型号",
  "serilanumName": "设备顺序",
  "equStatus": "设备角色",
  "picture": "实物ID",
  "moduleNum": "电源模块数量",
  "powerSupplyMode": "电源供电方式",
  "ethernetNum": "以太网口数量",
  "portNum": "2M口数量",
  "lightNum": "光口数量",
  "comboNum": "光电复用口数量",
}
import moment from "moment";
import navBar from "../../../../components/common/nav-bar";
import plateTitle from "../../../../components/business/plateTitle";
import {Dialog, Toast} from "vant";
import {mapGetters} from "vuex";

export default {
  name: 'routerVerify',
  components: {navBar,plateTitle },
  props: {},
  data() {
    return {
      taskDetail: {},
      buttonText: () => {
        if(this.isCoincide === "一致") return "保存";
        if(this.operation === "noExist") return "保存";
        if(this.operation === "modify") return "去完善台账信息";
      },
      isCoincide: "不一致",
      showPicker: false,
      columns: ["一致", "不一致"],
      operation: "modify",
      routerInfo:{},
      objId:'',
      taskSiteId:'',
      state:'',
      taskId:'',
      taskSiteName:'',
    };
  },
  computed: {
    ...mapGetters(['userData'])
  },
  watch: {},
  onLoad(options) {
    console.log(this.userData.userId,12312)
    console.log(this.userData.userName,12312)
    this.objId = options.objId
    this.taskSiteId = options.taskSiteId
    this.state = options.state
    this.taskId = options.taskId
    this.taskSiteName = options.taskSiteName
    this.getRouterBaseList()
  },
  methods: {

    scanClick() {
      let _this = this;
      wx.scanQRCode({
        desc:"scanQRCode desc",
        needResult: 1,
        scanType:["qrCode"],
        success:function(res){
          let scanCodeId = res.resultStr
          _this.getCheckEntityE(scanCodeId)
        },
        error:function(res){
          console.log("scanQRCode error",res);
        }
      });
    },
    // 核对
    getCheckEntityE(scanCodeId) {
      let params = {
        objId: this.objId,
        entityId: scanCodeId
      }
      this.$api.getCheckEntity(params).then(res => {
        if(res.data.flag === true) {
          Dialog.confirm({
            message: '信息匹配成功',
          }).then(() => {
          })
        }else {
          Dialog.confirm({
            title:'信息不匹配',
            message: '该设备实物ID为：'+ res.data.entityId,
          }).then(() => {
          })
        }
      })
    },
    getRouterBaseList () {
      this.$api.queryRouter({ routerid: this.objId }).then(res => {
        console.log('res.data---', res.data)
        if (res.data.length!=0){
          this.taskDetail = {
            originData: '',
            powerDetailArr: Object.keys(power_obj).map((item, index) => {
              return {
                title: power_obj[item],
                text: res.data[0][item]
              }
            })
          };
          this.routerInfo = res.data[0]
        }
      })
    },
    onConfirm(value) {
      this.isCoincide = value;
      this.showPicker = false;
    },
    confirmInfo () {
      // 选择一致或不存在时，保存提示
      if(this.isCoincide === "一致" || (this.isCoincide === "不一致" && this.operation === "noExist")) {
        Dialog.confirm({
          message: "所选巡视结果是否确认？确认后不可变更，如有问题请联系运维人员。",
        }).then(() => {

          if (this.isCoincide === "一致") {
            let checkSuccessParams = {
              LargeType: 'router',
              serviceState: 1, // （1 一致 2 不存在 3 新增 4 更新）
              tourOperId: this.userData.userId,
              tourOperName: this.userData.userName,
              routerid: this.objId,
            }
            this.$api.updateServicestate(checkSuccessParams).then(res => {
              if(res.code === 0) {

                Toast.success({
                  message: '保存成功',
                  onOpened: () => {
                    Dialog.confirm({
                      message: '是否继续核查业务信息？'
                    }).then(() => {
                      uni.navigateTo({
                        url: `/pages/workBench/todoTask/routerPatrol/routerOperationalVerification?objId=${this.objId}&taskId=${this.taskId}&state=${this.state}&taskSiteId=${this.taskSiteId}&taskSiteName=${this.taskSiteName}`
                      });
                    }).catch(() => {
                      uni.navigateTo({
                        url: `/pages/workBench/todoTask/routerPatrol/routerPatrol?objId=${this.objId}&taskId=${this.taskId}&state=${this.state}&taskSiteId=${this.taskSiteId}&taskSiteName=${this.taskSiteName}`
                      });
                    })

                  }
                });
              }
              else {
                Toast.fail("操作失败");
              }
            })
          }

          if (this.isCoincide === "不一致" && this.operation === "noExist") { // 认定设备不存在时进行保存
            let checkExistParams = {
              LargeType: 'router',
              serviceState: 2, // （1 一致 2 不存在 3 新增 4 更新）
              tourOperId: this.userData.userId,
              tourOperName: this.userData.userName,
              routerid: this.objId,
            }
            this.$api.updateServicestate(checkExistParams).then(res => {
              if(res.code === 0) {
                  Toast.success({
                    message: '保存成功',
                    onOpened: () => {
                      uni.navigateTo({
                        url: `/pages/workBench/todoTask/routerPatrol/routerPatrol?objId=${this.objId}&taskId=${this.taskId}&state=${this.state}&taskSiteId=${this.taskSiteId}&taskSiteName=${this.taskSiteName}`
                      });
                    }
                  });
                }

              else {
                Toast.fail("操作失败");
              }
            })
          }
        }).catch(() => {})
      }


      if (this.isCoincide === "不一致" && this.operation === 'modify') { // 认定设备存在但是需要完善信息
        let checkSuccessParams = {
          LargeType: 'router',
          serviceState: 4, // （1 一致 2 不存在 3 新增 4 更新）
          tourOperId: this.userData.userId,
          tourOperName: this.userData.userName,
          routerid: this.objId,
        }
        this.$api.updateServicestate(checkSuccessParams).then(res => {
          if(res.code === 0) {
            uni.navigateTo({
              url: `/pages/workBench/todoTask/routerPatrol/routerInformationPerfection?type=update&objId=${this.objId}&taskId=${this.taskId}&state=${this.state}&taskSiteId=${this.taskSiteId}&taskSiteName=${this.taskSiteName}`
            })
          }
          else {
            Toast.fail("操作失败");
          }
        })

      }
    },
    goBack() {
      uni.navigateTo({
        url:`/pages/workBench/todoTask/routerPatrol/routerPatrol?objId=${this.objId}&taskSiteId=${this.taskSiteId}&state=${this.taskType}&taskId=${this.taskId}&taskSiteName=${this.taskSiteName}`
      })
    },


  }
};
</script>
<style scoped lang="scss">
.router-inspect-main {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  .header {
    position: sticky;
    top: 0;
    z-index: 9;
  }
  .main-container {
    max-height: calc(100vh - #{px2vh(483)});
    overflow: auto;
    padding: px2vh(30) px2vw(30) px2vh(60) px2vw(30);
    background-color: #F8F8F8;
    /deep/.van-field__label,
    /deep/.van-field__control:read-only,
    /deep/.van-field__body .van-field__control,
    /deep/.van-cell-group .van-cell {
      font-size: px2vw(42);
    }
    /deep/.van-field__label {
      width: auto;
    }
    .base-info-container {
      border-radius: $border-radius-base;
      overflow: hidden;
      background-color: #FFFFFF;
      margin-bottom: px2vh(30);
      .operation {
        margin-left: px2vw(30);
        color: #417ADC;
        img {
          width: px2vw(42);
          vertical-align: text-top;
        }
      }
    }
    .patrol-info-container {
      font-size: px2vw(36);
      color: #666666;
      border-radius: $border-radius-base;
      overflow: hidden;
      background-color: #FFFFFF;
      .van-radio-group {
        padding: px2vh(60) px2vw(42);
      }
    }
    .plan-info-title {
      width: calc(100% - #{px2vw(40)});
      margin-left: px2vw(20);
      margin-bottom: px2vh(30);
      .palant-text {
        color: #3F9EFF;
        font-size: px2vh(36);
      }
    }
    .router-buinsess {
      height: auto;
      border-radius: px2vh(20);
      background: #fff;
      margin-bottom: px2vh(20);
      display: flex;
      flex-direction: column;
      justify-content: center;
      font-size: px2vh(36);
      .row {
        display: flex;
        align-items: center;
        line-height:  px2vh(80);
        .left-title {
          width: px2vw(300);
          text-align: right;
        }
        .left-title-value {
        }
      }

    }
  }
  /deep/.van-button {
    margin-top: px2vh(60);
    height: px2vh(120);
    color: #fff;
    background-color: #417ADC;
    border: 1px solid #417ADC;
    border-radius: px2vh(60);
  }
  /deep/ .van-cell__title {
    font-size: px2vh(36);
  }
}
</style>
